package com.hr.classes.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.hr.classes.entity.dto.TbWorkRecordStatisticsDTO;
import com.hr.classes.entity.po.TbWorkRecordPO;
import com.hr.classes.entity.vo.TbWorkRecordVO;
import com.hr.classes.service.TbWorkRecordService;
import com.hr.common.retrun.R;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * <pre>
 * 描述：TODO
 * </pre>
 *
 * @类名：com.hr.classes.controller.TbWorkRecordController
 * @创建日期: 2024/5/7 21:20
 */
@RestController
@CrossOrigin
@Api(tags = "考勤记录管理")
@RequestMapping("/tb/work/record")
public class TbWorkRecordController {

    @Autowired
    private TbWorkRecordService tbWorkRecordService;

    @GetMapping("/list")
    public R getList(TbWorkRecordVO tbWorkRecordVO) {
        return R.ok(tbWorkRecordService.getList(tbWorkRecordVO));
    }

    @PostMapping("/add")
    public R add(@RequestBody TbWorkRecordVO tbWorkRecordVO) {
        return R.ok(tbWorkRecordService.add(tbWorkRecordVO));
    }

    @GetMapping("/get")
    public R get(@RequestParam Integer id) {
        return R.ok(tbWorkRecordService.get(id));
    }

    @PostMapping("/update")
    public R update(@RequestBody TbWorkRecordVO tbWorkRecordVO) {
        return R.ok(tbWorkRecordService.update(tbWorkRecordVO));
    }

    @PostMapping("/deletes")
    public R deletes(@RequestBody List<Integer> ids) {
        return R.ok(tbWorkRecordService.deletes(ids));
    }

    @GetMapping("/exportExcel")
    public void exportExcel(TbWorkRecordVO tbWorkRecordVO, HttpServletResponse response) throws IOException {
        String sheetName = "sheet名";
        String fileName = "导出数据.xlsx";
        response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");

        List<TbWorkRecordPO> dataList = tbWorkRecordService.getListPO(tbWorkRecordVO);
        EasyExcel.write(response.getOutputStream(), TbWorkRecordPO.class)
                .autoCloseStream(false)
                .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).sheet(sheetName).doWrite(dataList);
    }

    @GetMapping("/statistics")
    public R statistics(TbWorkRecordStatisticsDTO tbWorkRecordStatisticsDTO) {
        return R.ok(tbWorkRecordService.statistics(tbWorkRecordStatisticsDTO));
    }

}
